package com.jiuqi.component.report.ezreport;

import java.io.File;
import java.io.FileFilter;
import java.io.FileWriter;
import java.io.IOException;

import org.exolab.castor.mapping.MappingException;
import org.exolab.castor.tools.MappingTool;

import com.jiuqi.common.StringUtils;

public class GenCastorXML {

	public static void genXML(String clsPath,String xmlPath,String pkgName){
			
		File f = new File(clsPath);
		
		  //File f = new File(EiConsts.MAP_CLASSFORM_PATH);
	        
		File[] fs = f.listFiles(new FileFilter() {

				public boolean accept(File arg0) {
					if (arg0.isDirectory())
	                    return true;
	                else if (arg0.getName().indexOf(".class") == -1)
	                    return false;
	                else
	                    return true;
	            }

	        });
	
	       //String packageName = "com.jiuqi.ccp.formbean.form.";
		for(int i=0;i<fs.length;i++){
        	if (fs[i].isFile()) {
        		try {
                    String fullName = fs[i].getName();
                    String[] name = StringUtils.split(fullName, ".", -1);
                    String prefixName = name[0];
                    File tf = new File(xmlPath+prefixName+".xml");
                    tf.createNewFile();
                    FileWriter fw= new FileWriter(tf);
                    MappingTool mt = new MappingTool();
                    mt.addClass(Class.forName(pkgName+prefixName),false);
                    mt.write(fw);
                    fw.flush();
                    fw.close();
                    } catch (MappingException e) {
                            e.printStackTrace();
                    } catch (ClassNotFoundException e) {
							e.printStackTrace();
					} catch (IOException e) {
						e.printStackTrace();
					}
        	}
        }
	}
}

